package com.shiningstars.php;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.valuebeans.Audit;
import com.shiningstars.valuebeans.Feedback;
import com.shiningstars.valuebeans.MsLocation;

public class WebHelper implements Constants, Config {

	public JSONObject registerUser(Map<String, String> dataMap, Context context) throws IllegalStateException,
			IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, REGISTER));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, dataMap.get(MOBILE_NUMBER)));
		params.add(new BasicNameValuePair(IMEI_NUMBER, dataMap.get(IMEI_NUMBER)));
		params.add(new BasicNameValuePair(VERSION, dataMap.get(VERSION)));
		params.add(new BasicNameValuePair(ISPROD, IS_PROD ? Y : N));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getUserStatus(Map<String, String> dataMap, Context context) throws IllegalStateException,
			IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, STATUS));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, dataMap.get(MOBILE_NUMBER)));
		params.add(new BasicNameValuePair(IMEI_NUMBER, dataMap.get(IMEI_NUMBER)));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject updateAppStatus(Map<String, String> dataMap, Context context) throws IllegalStateException,
			IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, APP_STATUS));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, dataMap.get(MOBILE_NUMBER)));
		params.add(new BasicNameValuePair(IMEI_NUMBER, dataMap.get(IMEI_NUMBER)));
		params.add(new BasicNameValuePair(STATUS, dataMap.get(STATUS)));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject audit(Audit audit, Context context) throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, AUDIT));
		params.add(new BasicNameValuePair(TYPE, audit.getType()));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, audit.getMobileNumber()));
		params.add(new BasicNameValuePair(IMEI_NUMBER, audit.getImeiNumber()));
		params.add(new BasicNameValuePair(DESCRIPTION, audit.getDescription()));
		params.add(new BasicNameValuePair(VERSION, audit.getVersion()));
		params.add(new BasicNameValuePair(CREATED_DATE, String.valueOf(audit.getDate())));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject sendFeedback(Feedback feedback, Context context) throws IllegalStateException, IOException,
			JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, FEEDBACK_SEND));
		params.add(new BasicNameValuePair(ID, String.valueOf(feedback.getId())));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, feedback.getMobileNumber()));
		params.add(new BasicNameValuePair(IMEI_NUMBER, feedback.getImeiNumber()));
		params.add(new BasicNameValuePair(CREATED_DATE, String.valueOf(feedback.getDate())));
		params.add(new BasicNameValuePair(MODE, feedback.getMode()));
		params.add(new BasicNameValuePair(IS_SYNCED, feedback.getIsSynced()));
		params.add(new BasicNameValuePair(IS_NOTIFIED, feedback.getIsNotified()));
		params.add(new BasicNameValuePair(QUESTION + "1", feedback.getQuestion1()));
		params.add(new BasicNameValuePair(QUESTION + "2", feedback.getQuestion2()));
		params.add(new BasicNameValuePair(QUESTION + "3", feedback.getQuestion3()));
		params.add(new BasicNameValuePair(QUESTION + "4", feedback.getQuestion4()));
		params.add(new BasicNameValuePair(QUESTION + "5", feedback.getQuestion5()));
		params.add(new BasicNameValuePair(QUESTION + "6", feedback.getQuestion6()));
		params.add(new BasicNameValuePair(QUESTION + "7", feedback.getQuestion7()));
		params.add(new BasicNameValuePair(QUESTION + "8", feedback.getQuestion8()));
		params.add(new BasicNameValuePair(QUESTION + "9", feedback.getQuestion9()));
		params.add(new BasicNameValuePair(QUESTION + "10", feedback.getQuestion10()));
		params.add(new BasicNameValuePair(QUESTION + "11", feedback.getQuestion11()));
		params.add(new BasicNameValuePair(QUESTION + "12", feedback.getQuestion12()));
		params.add(new BasicNameValuePair(QUESTION + "13", feedback.getQuestion13()));
		params.add(new BasicNameValuePair(QUESTION + "14", feedback.getQuestion14()));
		params.add(new BasicNameValuePair(QUESTION + "15", feedback.getQuestion15()));
		params.add(new BasicNameValuePair(QUESTION + "16", feedback.getQuestion16()));
		params.add(new BasicNameValuePair(QUESTION + "17", feedback.getQuestion17()));
		params.add(new BasicNameValuePair(QUESTION + "18", feedback.getQuestion18()));
		params.add(new BasicNameValuePair(QUESTION + "19", feedback.getQuestion19()));
		params.add(new BasicNameValuePair(QUESTION + "20", feedback.getQuestion20()));
		params.add(new BasicNameValuePair(QUESTION + "21", feedback.getQuestion21()));
		params.add(new BasicNameValuePair(QUESTION + "22", feedback.getQuestion22()));
		params.add(new BasicNameValuePair(QUESTION + "23", feedback.getQuestion23()));
		params.add(new BasicNameValuePair(QUESTION + "24", feedback.getQuestion24()));
		params.add(new BasicNameValuePair(QUESTION + "25", feedback.getQuestion25()));
		params.add(new BasicNameValuePair(QUESTION + "26", feedback.getQuestion26()));
		params.add(new BasicNameValuePair(QUESTION + "27", feedback.getQuestion27()));
		params.add(new BasicNameValuePair(QUESTION + "28", feedback.getQuestion28()));
		params.add(new BasicNameValuePair(QUESTION + "29", feedback.getQuestion29()));
		params.add(new BasicNameValuePair(QUESTION + "30", feedback.getQuestion30()));
		params.add(new BasicNameValuePair(QUESTION + "31", feedback.getQuestion31()));
		params.add(new BasicNameValuePair(QUESTION + "32", feedback.getQuestion32()));
		params.add(new BasicNameValuePair(QUESTION + "33", feedback.getQuestion33()));
		params.add(new BasicNameValuePair(QUESTION + "34", feedback.getQuestion34()));
		params.add(new BasicNameValuePair(QUESTION + "35", feedback.getQuestion35()));
		params.add(new BasicNameValuePair(QUESTION + "36", feedback.getQuestion36()));
		params.add(new BasicNameValuePair(QUESTION + "37", feedback.getQuestion37()));
		params.add(new BasicNameValuePair(QUESTION + "38", feedback.getQuestion38()));
		params.add(new BasicNameValuePair(QUESTION + "39", feedback.getQuestion39()));
		params.add(new BasicNameValuePair(QUESTION + "40", feedback.getQuestion40()));
		params.add(new BasicNameValuePair(QUESTION + "41", feedback.getQuestion41()));
		params.add(new BasicNameValuePair(QUESTION + "42", feedback.getQuestion42()));
		params.add(new BasicNameValuePair(QUESTION + "43", feedback.getQuestion43()));
		params.add(new BasicNameValuePair(QUESTION + "44", feedback.getQuestion44()));
		params.add(new BasicNameValuePair(QUESTION + "45", feedback.getQuestion45()));
		params.add(new BasicNameValuePair(QUESTION + "46", feedback.getQuestion46()));
		params.add(new BasicNameValuePair(QUESTION + "47", feedback.getQuestion47()));
		params.add(new BasicNameValuePair(QUESTION + "48", feedback.getQuestion48()));
		params.add(new BasicNameValuePair(QUESTION + "49", feedback.getQuestion49()));
		params.add(new BasicNameValuePair(QUESTION + "50", feedback.getQuestion50()));
		params.add(new BasicNameValuePair(QUESTION + "51", feedback.getQuestion51()));
		params.add(new BasicNameValuePair(QUESTION + "52", feedback.getQuestion52()));
		params.add(new BasicNameValuePair(QUESTION + "53", feedback.getQuestion53()));
		params.add(new BasicNameValuePair(QUESTION + "54", feedback.getQuestion54()));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject notifyFeedback(Feedback feedback, Context context) throws IllegalStateException, IOException,
			JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, FEEDBACK_NOTFIY));
		params.add(new BasicNameValuePair(FEEDBACK_ID, String.valueOf(feedback.getFeedbackId())));
		params.add(new BasicNameValuePair(IS_NOTIFIED, feedback.getIsNotified()));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getAdminCenters(String mobileNumber, Context context) throws IllegalStateException, IOException,
			JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, ADMIN_CENTERS));
		params.add(new BasicNameValuePair(MOBILE_NUMBER, mobileNumber));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getAdminFeedback(String feedbackIds, String centerNames, Context context)
			throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, ADMIN_FEEDBACK));
		params.add(new BasicNameValuePair(FEEDBACK_IDS, feedbackIds));
		params.add(new BasicNameValuePair(CENTER_NAMES, centerNames));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject sendMsLocation(MsLocation msLocation, Context context) throws IllegalStateException, IOException,
			JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, MS_LOCATION_SEND));
		params.add(new BasicNameValuePair(MS_LOCATION_ID, String.valueOf(msLocation.getMsLocationId())));
		params.add(new BasicNameValuePair(PLACE, msLocation.getPlace()));
		params.add(new BasicNameValuePair(STAY_DURATION, msLocation.getStayDuration()));
		params.add(new BasicNameValuePair(CONTACT_PERSON, msLocation.getContactPerson()));
		params.add(new BasicNameValuePair(COMMENTS, msLocation.getComments()));
		params.add(new BasicNameValuePair(POSTED_BY, msLocation.getPostedBy()));
		params.add(new BasicNameValuePair(POSTED_ON, msLocation.getPostedOn()));
		params.add(new BasicNameValuePair(CREATED_DATE, String.valueOf(msLocation.getCreatedDate())));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getMsLocation(long msLocationId, Context context) throws IllegalStateException, IOException,
			JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, MS_LOCATION));
		params.add(new BasicNameValuePair(MS_LOCATION_ID, String.valueOf(msLocationId)));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getNewParseDetails(String objectId, String installationId, Context context)
			throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, GET_PARSE_DETAILS));
		params.add(new BasicNameValuePair(OBJECT_ID, objectId));
		params.add(new BasicNameValuePair(INSTALLATION_ID, installationId));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject insertParseDetails(String objectId, String installationId, Context context)
			throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, INSERT_PARSE_DETAILS));
		params.add(new BasicNameValuePair(OBJECT_ID, objectId));
		params.add(new BasicNameValuePair(INSTALLATION_ID, installationId));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject updateParseDetails(String objectId, String installationId, Context context)
			throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, UPDATE_PARSE_DETAILS));
		params.add(new BasicNameValuePair(OBJECT_ID, objectId));
		params.add(new BasicNameValuePair(INSTALLATION_ID, installationId));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getLatestVersionDetails(Context context) throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, LATEST_APP_VERSION));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}

	public JSONObject getActiveTeachers(Context context) throws IllegalStateException, IOException, JSONException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG, ACTIVE_TEACHERS));

		JSONParser jsonParser = new JSONParser();
		JSONObject json = jsonParser.getJSONFromUrl(context, URL, params);

		return json;
	}
}